package movie.test.aop;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import movie.test.as.user.bean.AdminUser;
import movie.test.as.user.bean.MovieUserInfo;

public class AuthorInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// System.out.println("拦截器执行");///movie.front/adminUserController/login
		// 获取请求的url
		String url = request.getRequestURI();
		// 判断url是否公开地址（实际使用时将公开地址配置到配置文件中）
		// 这里假设公开地址是否登陆提交的地址
		// System.out.println(url);
		if (url.indexOf("login") < 0) {
			// 如果进行登陆提交，放行
			/* return true; */
			// 判断session
			HttpSession session = request.getSession();
			// 从session中取出用户身份信息
			AdminUser users = (AdminUser) session.getAttribute("LOGIN_ADMIN_USER");
			if (users != null) {
				return true;
			} else {

				// 执行到这里表示用户身份需要验证，跳转到登陆页面
				/*
				 * response.setContentType("text/json;charset=UTF-8");
				 * response.setCharacterEncoding("UTF-8");
				 * response.getWriter().write("访问受限");
				 */
			/*	String url1 = "/movie.web";
				System.out.println(request.getContextPath());
				response.sendRedirect("http://localhost:8080/movie.web/index.html");*/
				return false;

			}
		}

		return true;

	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		/*
		 * MovieUserInfo user =(MovieUserInfo)
		 * request.getSession().getAttribute("LOGIN_USER"); if(user==null){
		 * response.sendRedirect("http://localhost:8080/movie.web/index.html");
		 * }
		 */

		System.out.println("第二个配置");

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}

}
